//查找公司
function search() {
  let input = document.getElementById("myInput");
  let filter = input.value.toUpperCase();
  let table = document.getElementById("myTable");
  let tr = table.querySelectorAll("tr");
  // 循环表格每一行，查找匹配项
  for (let i = 0; i < tr.length; i++) {
    let td = tr[i].querySelectorAll("td")[0]; //获得第一列（公司名称）
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
let input = document.getElementById("myInput");
input.addEventListener("input", search);

//对公司升序操作
let sheng = document.getElementById("sheng");

sheng.onclick = () => {
  let table = document.getElementById("myTable");
  let tr = table.querySelectorAll("tr");
  let sum = [0, 0, 0, 0];
  let j = 0;
  let some;
  for (let i = 0; i < tr.length; i++) {
    let td = tr[i].querySelectorAll("td")[0];
    if (td) {
      sum[j] = td.innerHTML.toUpperCase()[0].charCodeAt();
      //console.log(sum[j]);
      j++;
    }
  }
  for (let m = 0; m < 3; m++) {
    for (let n = 0; n < 3 - m; n++) {
      if (sum[n] > sum[n + 1]) {
        some = tr[n + 1].innerHTML;
        tr[n + 1].innerHTML = tr[n + 2].innerHTML;
        tr[n + 2].innerHTML = some;
      }
    }
  }
  //console.log(sum[0]);
  // let a= td.innerHTML.toUpperCase()[0].charCodeAt();
  //     console.log(a);
};

//对公司降序操作
let jiang = document.getElementById("jiang");

jiang.onclick = () => {
  let table = document.getElementById("myTable");
  let tr = table.querySelectorAll("tr");
  let sum = [0, 0, 0, 0];
  let j = 0;
  let some;
  for (let i = 0; i < tr.length; i++) {
    let td = tr[i].querySelectorAll("td")[0];
    if (td) {
      sum[j] = td.innerHTML.toUpperCase()[0].charCodeAt();
      //console.log(sum[j]);
      j++;
    }
  }
  for (let m = 0; m < 3; m++) {
    for (let n = 0; n < 3 - m; n++) {
      if (sum[n] < sum[n + 1]) {
        some = tr[n + 1].innerHTML;
        tr[n + 1].innerHTML = tr[n + 2].innerHTML;
        tr[n + 2].innerHTML = some;
      }
    }
  }
};
